package com.kaishengit.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.kaishengit.pojo.TaskType;
import com.kaishengit.pojo.User;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.TaskTypeService;
import com.opensymphony.xwork2.ActionSupport;

public class TaskTypeAction extends ActionSupport implements ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private HttpServletRequest request;
	private TaskTypeService taskTypeService;
	private TaskType type2;
	private String id;
	private String color;
	public String execute(){
		return "success";
	}
	//添加一个类别
	public String add() throws UnsupportedEncodingException{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		name = new String(name.getBytes("Iso8859-1"),"utf-8");
		type2 = taskTypeService.save(user,name);
		//type2.setUser(null);
		System.out.println(type2.getColor());
		List<TaskType> list = taskTypeService.findTypeByUser(user);
		session.setAttribute("taskTypeList",list);
		return SUCCESS;
		
	}
	public String del(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		taskTypeService.del(id);
		List<TaskType> list = taskTypeService.findTypeByUser(user);
		session.setAttribute("taskTypeList",list);
		return "success";
	}
	public String update(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		type2 = taskTypeService.findById(id);
		type2.setColor(color);
		taskTypeService.update(type2);
		List<TaskType> list = taskTypeService.findTypeByUser(user);
		session.setAttribute("taskTypeList",list);
		return SUCCESS;
	}
	public String update2() throws UnsupportedEncodingException{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		type2 = taskTypeService.findById(id);
		name = new String(name.getBytes("Iso8859-1"),"utf-8");
		type2.setTypeContent(name);
		taskTypeService.update(type2);
		List<TaskType> list = taskTypeService.findTypeByUser(user);
		session.setAttribute("taskTypeList",list);
		return SUCCESS;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public void setTaskTypeService(TaskTypeService taskTypeService) {
		this.taskTypeService = taskTypeService;
	}
	public TaskType getType2() {
		return type2;
	}
	public void setType2(TaskType type2) {
		this.type2 = type2;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
	

}
